package com.ucampus.service;

import com.ucampus.dao.userinfoMapper;
import com.ucampus.entity.ManagerUser;
import com.ucampus.entity.ResponseData;
import com.ucampus.entity.userinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.List;

/**
 * 功能：
 * 作者： 启明星_Polaris
 * 日期：2024/12/26 下午10:40
 **/

@Service
public class userinfoServiceImpl implements userinfoService{
    @Autowired
    private userinfoMapper userinfoMapper;


    @Override
    public List<userinfo> getAllUsers(){
        return userinfoMapper.getAllUsers();
    }

    @Override
    public userinfo getUserById(long id) {
        return userinfoMapper.getUserById(id);
    }

    @Override
    public boolean updateUser(userinfo user) {
        // 校验是否传递了更新字段
        if (user == null || user.getId() == 0) {
            throw new IllegalArgumentException("Invalid user data");
        }

        // 检查是否至少有一个字段被更新
        if (user.getUsername() == null &&
                user.getUserAvatar() == null &&
                user.getUserGender() == null &&
                user.getUserBirthday() == null &&
                user.getUserNation() == null &&
                user.getUserPhone() == null &&
                user.getUserEmail() == null &&
                user.getUserAddress() == null &&
                user.getUserLive() == null &&
                user.getUserBio() == null) {
            throw new IllegalArgumentException("No fields to update");
        }

        if(user.getUserBirthday() != null) {
            try {
                String isoDate = user.getUserBirthday(); // 假设前端传入 ISO 格式日期
                LocalDate localDate = ZonedDateTime.parse(isoDate).toLocalDate(); // 解析为 LocalDate
                String formattedDate = localDate.toString(); // 转换为 yyyy-MM-dd 格式
                user.setUserBirthday(formattedDate); // 设置为格式化后的日期
            } catch (Exception e) {
                throw new IllegalArgumentException("Invalid date format for userBirthday");
            }
        }

        int rowsAffected = userinfoMapper.updateUser(user);
        return rowsAffected > 0;
    }

    @Override
    public void deleteUser(long id) {
         userinfoMapper.deleteUser(id);
    }

    @Override
    public ResponseData delete(List<Integer> list) {
        ResponseData responseData = new ResponseData();
        int result = userinfoMapper.delete(list);
        if (result > 0) {
            responseData.setMessage("删除成功,"+result/2+"名用户已删除，请刷新页面获取最新数据");
        }else {
            responseData.setMessage("删除过程出现问题");
        }
        return responseData;
    }

    @Override
    public ResponseData getUsers() {
        ResponseData responseData = new ResponseData();
        List<ManagerUser> list = userinfoMapper.getUsers();
        if(!list.isEmpty()) {
            responseData.setMessage("查询成功");
            responseData.setData(list);
        }else {
            responseData.setMessage("查询失败，请重试");
        }
        return responseData;
    }

    @Override
    public ResponseData getUsersByName(String username) {
        ResponseData responseData = new ResponseData();
        List<ManagerUser> list = userinfoMapper.getUsersByName(username);
        if(!list.isEmpty()) {
            responseData.setMessage("查询成功");
            responseData.setData(list);
        }else {
            responseData.setMessage("查询失败，请重试");
        }
        return responseData;
    }

    @Override
    public ResponseData updateStatus(List<Integer> list) {
        ResponseData responseData = new ResponseData();
        int result = userinfoMapper.updateStatus(list);
        if (result > 0) {
            responseData.setMessage("修改成功,"+result+"行数据已修改，请刷新页面获取最新数据");
        }else {
            responseData.setMessage("修改过程出现问题");
        }
        return responseData;
    }
}
